# -*- encoding: utf-8 -*-

# Copyright 2010 István Szentandrási
# <etude-music-player@googlegroups.com>

# This file is part of Etude.
#
# Etude is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as 
# published by the Free Software Foundation, either version 3 
# of the License, or (at your option) any later version.
#
# Etude is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Etude. If not, see <http://www.gnu.org/licenses/>.

import xmmsclient

from common import *
from equalizer import Equalizer


class Plugins(dict):

    """
    class for storing wrappers around xmms2 plugins
    """

    __supported = {u'equalizer': Equalizer}

    def __init__(self, client):
        dict.__init__(self)
        self.client = client

    @check_fast(False)
    @check_wait(False)
    def load(self):
        return self.client.xmms.plugin_list( xmmsclient.PLUGIN_TYPE_ALL, self.__load_plugins_cb)

    @check_result_error()
    def __load_plugins_cb(self, result):
        for d in result.value():
            shname = unicode(d['shortname'])
            self[shname] = d
            if shname in self.__supported.keys():
                self[shname]['instance'] = (self.__supported[shname])(self.client)
                #print("plugin_load_emitted", shname)
                self.client.emit("e_plugin_loaded", shname)

    def unload(self):
        plugins = [shname for shname in self.keys()]
        for shname in plugins:
            self.client.emit("e_plugin_unloaded", shname)
            del self[shname]

